from operator import itemgetter, attrgetter
student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),]


class Student:
    def __init__(self, name, grade, age):
        self.name = name
        self.grade = grade
        self.age = age

    def __repr__(self):
        return repr((self.name, self.grade, self.age))


student_objects = [Student('john', 'A', 15), Student('jane', 'B', 12),Student('dave', 'B', 10),]

print(sorted(student_tuples, key=lambda student: student[2])) # sort by age
print(sorted(student_objects, key=lambda student: student.age))   # sort by age

print(sorted({1: 'D', 6: 'B', 3: 'B', 4: 'E', 5: 'A'}))
print(sorted(student_tuples, key=itemgetter(2)))
print(sorted(student_objects, key=attrgetter('age')))
print(sorted(student_tuples, key=itemgetter(1,2)))
print(sorted(student_objects, key=attrgetter('grade', 'age')))

data = [('red', 1), ('blue', 3), ('red', 2), ('blue', 2)]
print(sorted(data, key=itemgetter(0)))


# sort the student data by descending grade and then ascending age, do the age sort first and then sort again using grade:
s = sorted(student_objects, key=attrgetter('age'))    # sort on secondary key
print(sorted(s, key=attrgetter('grade'), reverse=True) )      # now sort on primary key, descending
